package com.smartrodin.springbootrabbitmq.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.smartrodin.springbootrabbitmq.annotation.LocalLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

/**
 * @author: arnold_nie
 * @date: 2020/12/18 11:03
 * FileName: ManyApiTest
 * Description:  一次请求多个 AIP 测试
 */
@Slf4j
@RestController
@RequestMapping("/manyApi")
public class ManyApiTest {

    /**
     * num1
     * @param map
     * @return
     */
    @RequestMapping(value = "/num1", method = RequestMethod.POST)
    public Map<String, String> num1(@RequestBody Map<String, String> map) {
        log.info( "num1=============> {}", map);
        Random r = new Random();
        int i = r.nextInt( 100 );
        int i2 = r.nextInt( 100 );
        map.put( "num1",String.valueOf( i ) );
        map.put( "num2",String.valueOf( i2 ) );
        return map;
    }

    /**
     * num2
     * @param map
     * @return
     */
    @RequestMapping(value = "/num2", method = RequestMethod.POST)
    public Map<String, String> num2(@RequestBody Map<String, String> map) {
        log.info( "num2=============> {}", map);
        map.put( "uuid", UUID.randomUUID().toString() );
        return map;
    }

    /**
     * num3
     * @param map
     * @return
     */
    @RequestMapping(value = "/num3", method = RequestMethod.POST)
    public Map<String, String> num3(@RequestBody Map<String, String> map) {
        log.info( "num3=============> {}", map);
        map.put( "uuid2", UUID.randomUUID().toString() );
        return map;
    }

    @LocalLock(key = "city:arg[0]")
    @RequestMapping(value = "/testLock", method = RequestMethod.POST)
    public Map<String, String> testLock(@RequestBody Map<String, String> map) {
        log.info( "num3=============> {}", map);
        map.put( "uuid2", UUID.randomUUID().toString() );
        return map;
    }

    @LocalLock(key = "city:arg[id]")
    @RequestMapping(value = "/testLock2", method = RequestMethod.POST)
    public Map<String, String> testLock2(@RequestBody Map<String, String> map) {
        log.info( "num3=============> {}", map);
        map.put( "uuid2", UUID.randomUUID().toString() );
        ThreadLocalRandom random = ThreadLocalRandom.current();
        random.nextLong(100);
        return map;
    }

    public static void main(String[] args) {
//        System.out.println(new Random().nextInt( 100 ));
        JSONArray array = JSON.parseArray("[{"id":"3415797","name":"BU","description":"Bean_Dou费用报销","priority":50,"owner":null,"assignee":"1002828","assigneeName":null,"processInstanceId":"3415759","executionId":"3415759","processDefinitionId":"expense_apply:9:3371900","processDefinitionName":"费用报销","processDefinitionKey":"expense_apply","createTime":"2021-01-13 18:26:20","taskDefinitionKey":"sid-7F3328DE-4110-40C6-8F07-F6A3CE5A6993","dueDate":null,"restDay":null,"category":null,"parentTaskId":null,"tenantId":"","applyUserId":"1003151","applyUserName":"Bean_Dou","applyOrgName":"Vanguard","applyCompanyName":null,"oldSettlementCode":null}]");
        StringBuffer sBuffer = new StringBuffer();
        array.stream().forEach(jsonobejct -> arrayIdToString((JSONObject) jsonobejct, sBuffer));
        System.out.println(sBuffer.toString());
        for(int i=0;i<array.size();i++) {
//            System.out.println(array.getJSONObject(i).get("executionId"));
        }
    }
    private static StringBuffer arrayIdToString(JSONObject jsonobejct,
                                                StringBuffer sBuffer) {
        return sBuffer.append(jsonobejct.getInteger("executionId")).append(",");
    }
}
